package com.mall.DataSourceConfig;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;
import java.util.Properties;


@Configuration
@MapperScan(basePackages = "com.mapper.second",sqlSessionTemplateRef = "secondSqlSessionTemplate")
public class SecondDataSource {

    @Value("${second.datasource.driver-class-name}")
    private String driverClassName;
    @Value("${second.datasource.jdbcUrl}")
    private String url;
    @Value("${second.datasource.username}")
    private String userName;
    @Value("${second.datasource.password}")
    private String passWord;


    @Bean(name = "secondData")
    @ConfigurationProperties(prefix = "second.datasource")
    public DataSource sentinelData() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driverClassName);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(userName);
        druidDataSource.setPassword(passWord);
        return druidDataSource;
    }

    @Bean(name = "secondSqlSessionFactory")
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("secondData") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            try {
            bean.setMapperLocations(resolver.getResources("classpath*:mappers/*.xml"));
                //分页插件
                PageHelper pageHelper = new PageHelper();
                Properties properties = new Properties();
                properties.setProperty("dialect", "mysql");
                properties.setProperty("reasonable", "false");
                properties.setProperty("offsetAsPageNum", "true");
                properties.setProperty("rowBoundsWithCount", "true");
                properties.setProperty("pageSizeZero", "true");
                properties.setProperty("params","pageNum=pageNumKey;pageSize=pageSizeKey;");
                pageHelper.setProperties(properties);
                bean.setPlugins(new Interceptor[]{pageHelper});
                return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Bean(name = "secondSqlSessionTemplate")
    public SqlSessionTemplate userSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
        return template;
    }

}
